﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using OpenComposite.Base.Designer;
using OpenComposite.EII.Designer;
using System.Windows.Forms;
using System.Drawing;
using OpenComposite.EII.RepositoryUIs;

namespace OpenComposite.EII.Repository
{
	//[System.Security.Permissions.PermissionSet(
	//    System.Security.Permissions.SecurityAction.Demand,
	//    Name = "FullTrust")]
	internal class RepositoryItemDesigner : IDesigner
	{
		#region Initialization
		public RepositoryItemDesigner()
		{
			_component = null;
		}
		#endregion

		#region IDesigner Members

		public System.ComponentModel.IComponent Component
		{
			get
			{
				return _component;
			}
		}

		public void DoDefaultAction()
		{
		}

		public void Initialize(System.ComponentModel.IComponent component)
		{
			if ( !( component is IRepositoryItem ) ) {
				throw new InvalidOperationException("The RepositoryItemDesigner can be initialized with a IRepositoryItem only.");
			}
			_component = component;
		}

		public DesignerVerbCollection Verbs
		{
			get { return new DesignerVerbCollection(); }
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			_component = null;
		}

		#endregion

		#region Public Members

		#region Properties
		public virtual bool HasControllers
		{
			get { return false; }
		}

		public virtual IRepositoryItem RepositoryItem
		{
			get { return _component as IRepositoryItem; }
		}
		#endregion

		#region Methods
		public virtual ItemType GetDesignerControlItemType()
		{
			ItemType itemtype = ItemType.Undefined;
			IRepositoryItem item = this.Component as IRepositoryItem;
			if ( item != null ) {
				itemtype = item.RepositoryItemType;
			}
			return itemtype;
		}
		public virtual object[] GetControllers(IController parentController)
		{
			return null;
		}
		public virtual Control GetEditor()
		{
			return null;
		}
		public virtual DialogResult ShowNewItemForm(int idRepository, bool bAllowContinue,
			IRepositoryItem parentItem, ref int idDomain, out IRepositoryItem parentItemNew)
		{
			FormCreateNewItem dlgNew = new FormCreateNewItem(this.RepositoryItem,
															 (RepositoryItemBase)this.RepositoryItem,
															 bAllowContinue);
			dlgNew.DomainID = idDomain;
			dlgNew.ShowAllFolders = false;
			dlgNew.ParentItem = parentItem;
			DialogResult result = dlgNew.ShowDialog();
			if ( result == DialogResult.OK || result == DialogResult.Retry ) {
				idDomain = dlgNew.DomainID;
				parentItemNew = dlgNew.NewParentItem;
			} else {
				idDomain = 0;
				parentItemNew = null;
			}
			return result;
		}

		public virtual void SetDragDropDataObject(IDataObject dataObj)
		{
			dataObj.SetData(this);
		}

		public virtual Icon GetIcon()
		{
			if ( this.RepositoryItem != null ) {
				return this.RepositoryItem.GetIcon();
			} else {
				return null;
			}
		}

		public virtual T GetService<T>() where T : class
		{
			if ( this.RepositoryItem != null ) {
				return this.RepositoryItem.GetService(typeof(T)) as T;
			} else if ( _component.Site != null ) {
				return _component.Site.GetService(typeof(T)) as T;
			} else {
				return null;
			}
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private IComponent _component;
		#endregion

		#endregion
	}
}
